﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;
using File = Controllers.File;

namespace Web
{
    public partial class AuthedList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            string idStr = Request.QueryString["id"];
            EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
            if (!string.IsNullOrEmpty(idStr))
            {
                var list = from r in ctx.Files where r.FileClass == 0 && r.CreatTime > DateTime.Now.AddDays(-40) && (from r3 in ctx.FileAuths where r3.ToId == ebi.Id && r3.FromId == int.Parse(idStr) select r3.FileId).Contains(r.Id) join r2 in ctx.Employees on r.OwnerId equals r2.Id orderby r.OrderNo, r.CreatTime descending select new { r.Id, r.Name, r.Size, EmpName = r2.Name, r.CreatTime, r.Type, r.FileClass, r.RealName };
                rptList.DataSource = list;
                rptList.DataBind();
            }
            else
            {
                var list = from r in ctx.Files where r.FileClass == 0 && r.CreatTime > DateTime.Now.AddDays(-40) && (from r3 in ctx.FileAuths where r3.ToId == ebi.Id select r3.FileId).Contains(r.Id) join r2 in ctx.Employees on r.OwnerId equals r2.Id orderby r.OrderNo, r.CreatTime descending select new { r.Id, r.Name, r.Size, EmpName = r2.Name, r.CreatTime, r.Type, r.FileClass, r.RealName };
                rptList.DataSource = list;
                rptList.DataBind();
            }
        }

        protected string getSize(object size)
        {
            string retStr = null;
            if (size != null && size != "0")
            {
                retStr = (int.Parse(size.ToString()) / 1024).ToString();
            }
            return retStr;
        }

        protected string getIcon(string type)
        {
            string retStr = type;
            string validExt = "com,doc,exe,gif,htm,jpg,ppt,rar,txt,xls,zip,dir";
            if (validExt.IndexOf(type) < 0)
            {
                retStr = "com";
            }
            return retStr;
        }

        protected string getLinks(string idStr, string type, string fileClass, object realName)
        {
            string retStr = "";
            if (type != "dir")
            {
                if ("xls,xlsx,doc,docx".IndexOf(type) >= 0)
                {
                    retStr += "<span onclick='view(" + fileClass + ",\"" + realName + "\"," + idStr + ");'>[查看]</span>";
                }
                retStr += "<span onclick='location.href=\"FileDownload.aspx?fileId=" + idStr + "\";'>[下载]</span>";
                retStr += "<span onclick='fileAuth(" + idStr + ");'>[授权]</span>";
                retStr += "<span onclick='ReadLog(" + idStr + ");'>[日志]</span>";
            }
            return retStr;
        }
    }
}